package com.foling.system.controller.mall;

import com.foling.system.common.Result;
import com.foling.system.entity.*;
import com.foling.system.service.MallOrderService;
import com.foling.system.service.OrderItemService;
import com.foling.system.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigInteger;
import java.util.Date;

@Controller
public class OrderController {
    @Autowired
    private MallOrderService mallOrderService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private ProductService productService;
    //跳转到选择支付方式页面
    @GetMapping({"/selectPay"})
    public String selectPayType(HttpServletRequest request,HttpSession httpSession) {
        //保存订单
        User user = (User) httpSession.getAttribute("User");
        Cart cart = (Cart) httpSession.getAttribute("cart");
        MallOrder mallOrder = new MallOrder();
        BigInteger bill = BigInteger.valueOf(System.currentTimeMillis());
        String string = bill.toString();
        string = string.substring(9);
        mallOrder.setBillNo(Integer.parseInt(string));
        mallOrder.setStatus("已下单");
        mallOrder.setCreateTime(new Date());
        mallOrder.setUserId(user.getUserId());
        mallOrder.setPrice(cart.getTotalPrice());
        for (CartItem cartItem:cart.getItems()
        ) {
            OrderItem orderItem = new OrderItem();
            orderItem.setTotalPrice(cartItem.getTotalPrice());
            orderItem.setCount(cartItem.getCount());
            orderItem.setProductPrice(cartItem.getPrice());
            orderItem.setProductId(cartItem.getProductId());
            orderItem.setProductName(cartItem.getProductName());
            orderItemService.save(orderItem);
            Product product = productService.getById(orderItem.getProductId());
            product.setCount(product.getCount()-cartItem.getCount());
            productService.saveOrUpdate(product);
        }
        mallOrderService.save(mallOrder);
        for (MallOrder order:mallOrderService.list()
        ) {
            if (order.getBillNo() == Integer.parseInt(string))
                mallOrder.setOrderId(order.getOrderId());
        }
        request.setAttribute("orderId",mallOrder.getOrderId());
        request.setAttribute("bill", bill);
        httpSession.setAttribute("orderId",mallOrder.getOrderId());
        return "mall/paySelect";
    }
    //选择支付方式
    @GetMapping("/payPage")
    public String payOrder(HttpSession httpSession, HttpServletRequest request, @RequestParam("payType") int payType) {
        Integer orderId = (Integer) httpSession.getAttribute("orderId");
        MallOrder mallOrder = mallOrderService.getById(orderId);
        mallOrder.setPayType(payType);
        mallOrderService.saveOrUpdate(mallOrder);
        httpSession.getAttribute("orderId");
        if (payType == 1)
            return "mall/alipay";
        else if (payType == 2)
            return "mall/wxpay";
        return "mall/cashPay";
    }
    @GetMapping("/paySuccess")
    @ResponseBody
    public Result paySuccess(@RequestParam("orderNo") String orderNo, @RequestParam("payType") int payType) {
        MallOrder mallOrder = mallOrderService.getById(Integer.parseInt(orderNo));
        mallOrder.setPayType(payType);
        mallOrder.setStatus("已支付");
        return new Result(200,"支付成功");
    }
}
